package org.cangku.controller;

import org.cangku.dao.LskucunDao;
import org.cangku.dao.ShopInfoDao;
import org.cangku.entity.Lskucun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/lskc")
public class LsKucunController {
    @Autowired
    private LskucunDao lskucunDao;
    @Autowired
    private ShopInfoDao shopInfoDao;

    /*清除session*/
    @RequestMapping("/qsession")
    @ResponseBody
    public Map<String,Object> qcSession(HttpServletRequest request){
        Map<String,Object> map = new HashMap<String, Object>();

        request.getSession().removeAttribute("dingdan");
        request.getSession().removeAttribute("zongqian");
        map.put("qing",1);
        return map;
    }

    /*根据单号获得信息*/
    @RequestMapping("/getInfoByDD")
    @ResponseBody
    public List<Lskucun> query(String danhao){
        return lskucunDao.queryInfoByDingdan(danhao);
    }

    @RequestMapping("/deleteByDingdan")
    @ResponseBody
    public Map<String,Object> deleteByDanhao(String danhoa){
        Map<String,Object> map = new HashMap<String, Object>();
        lskucunDao.deleteByDanHaoAndPid(danhoa);
        return map;
    }

    /*根据单号修改pdid 为1*/
    @RequestMapping("/updatePidBydh")
    @ResponseBody
    public Map<String,Object> updatePdidByDanhao(String danhao){
        Map<String,Object> map = new HashMap<String, Object>();
        int i = lskucunDao.updatePdidByDanHoa(danhao);
        if(i>0){
            map.put("row",i);
            map.put("lskList",lskucunDao.queryInfoByDingdan(danhao));
            map.put("yingfu",lskucunDao.getZongJine(danhao));
        }else{
            map.put("row",0);
        }
        return map;
    }

    @RequestMapping("/deleteByzizeng")
    @ResponseBody
    public Map<String,Object> deleteByZiZeng(int lsid,String danhao){
        Map<String,Object> map = new HashMap<String, Object>();
        int i = lskucunDao.deletByZiZeng(lsid);
        if(i>0){
            map.put("row",i);
            map.put("schlist",lskucunDao.queryInfoByddandpdid(danhao));
            map.put("pdidyisc",lskucunDao.queryInfoByDingdan(danhao));
            //map.put("money",lskucunDao.getZongJine(danhao));
        }else{
            map.put("row",0);
        }
        return map;
    }

    @RequestMapping("/deleteByzizeng2")
    @ResponseBody
    public Map<String,Object> deleteByZiZeng2(int lsid,String danhao){
        Map<String,Object> map = new HashMap<String, Object>();
        int i = lskucunDao.deletByZiZeng(lsid);
        if(i>0){
            map.put("row",i);
            map.put("schlist",lskucunDao.queryInfoByddandpdid(danhao));
            map.put("pdidyisc",lskucunDao.queryInfoByDingdan(danhao));
            map.put("money",lskucunDao.getZongJine(danhao));
        }else{
            map.put("row",0);
        }
        return map;
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Map<String,Object> inert(Lskucun lskucun,HttpServletRequest request){
        Map<String,Object> map = new HashMap<String, Object>();
        /*得到商品信息最高id*/
        int bianhao = shopInfoDao.getMaxBianHao();

        lskucun.setSid(bianhao);
        lskucun.setPdid(1);
        double zongqian = 0.0;
        zongqian = lskucun.getJhnum()*lskucun.getJinjia();
        lskucun.setZongjine(zongqian);
        int i = lskucunDao.extendsCunzaiyi(lskucun);
        if(i>0){
            lskucunDao.updateNumAndMoneyyi(lskucun);
            map.put("row",1);
            map.put("lslist",lskucunDao.queryInfoByDingdan(lskucun.getDingdan()));
            map.put("moeny",lskucunDao.getZongJine(lskucun.getDingdan()));
        }else{
            lskucunDao.insert(lskucun);
            map.put("row",1);
            map.put("lslist",lskucunDao.queryInfoByDingdan(lskucun.getDingdan()));
            map.put("moeny",lskucunDao.getZongJine(lskucun.getDingdan()));
        }

        return map;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(){
        Map<String,Object> map = new HashMap<String, Object>();
        lskucunDao.delete();
        map.put("rr",1);
        return map;
    }
}
